import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";

Vue.use(VueRouter);
import store from "../store/index.js";
//引入vuex的仓库

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: "/list",
    name: "list",
    component: () => import("../views/List.vue"),
  },
  {
    path: "/shopcar",
    name: "shopcar",
    component: () => import("../views/Shopcar.vue"),
  },
  {
    path: "/my",
    name: "my",
    component: () => import("../views/My.vue"),
  },
  {
    path: "/detail",
    name: "detail",
    component: () => import("../views/Detail.vue"),
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/Login.vue"),
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  if (store.state.user != "") {
    //先引入store
    next();
  } else {
    if (to.path == "/login") {
      next();
    } else {
      next("/login");
    }
  }
});
//全局守卫 守卫所有的页面

export default router;
